package com.web.demo.demo;


import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @author sw
 * @create 2023-05-17 12:09
 */

public class CglibMethodInterceptor implements MethodInterceptor {
    //主要的方法拦截类，它是Callback接口的子接口，需要用户实现

    @Override
    public Object intercept(Object obj, Method method, Object
            [] args, MethodProxy proxy) throws Throwable {
        System.out.println("before cgplib");
        Object result = null;
        try {
            //利用反射创建代理对象
            result = proxy.invokeSuper(obj, args);
        } catch (Exception e) {
            System.out.println("ex:" + e.getMessage());
            throw e;
        } finally {
            System.out.println("after cglib");
        }
        return result;
    }

}
